import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store'

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 白名单列表，用路由的name设置
export const WHITE_LIST_NAMES = ['404', 'login', 'dashboard', 'account']

export const constantRoutes = [
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { menuId: 1, icon: 'menu-dashboard' },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', menuId: 'M_0' }
      }
    ]
  },
  {
    path: '/pass-template',
    component: Layout,    
    hidden: true,
    meta: { noPadding: true },
    children: [
      {
        path: '',
        name: 'PassTemplate',
        component: () => import('@/views/pass-center/passTemplate.vue')
      }
    ]
  },
  {
    path: '/pass-center/pass-detail',
    component: Layout,
    children: [
      {
        path: '',
        name: 'PassDetail',
        component: () => import('@/views/pass-center/components/passDetail.vue')
      }
    ]
  },
  {
    path: '/pass-center/nfc',
    component: Layout,
    children: [
      {
        path: '',
        name: 'PassNfc',
        component: () => import('@/views/pass-center/nfc.vue')
      }
    ]
  },
  {
    path: '/pass-center/certificate',
    component: Layout,
    children: [
      {
        path: '',
        name: 'PassCertificate',
        component: () => import('@/views/pass-center/certificate.vue')
      }
    ]
  }
]

export const asyncRoutes = [
  {
    path: '/merchant/merchant-detail/:mrchId',
    component: Layout,
    children: [
      {
        path: '',
        name: 'merchant-detail',
        component: () => import('@/views/merchant/merchant-detail.vue')
      }
    ]
  },
  {
    path: '/pass-settings',
    component: Layout,
    children: [{
      path: '',
      name: 'PassSettings',
      component: () => import('@/views/pass-center/nfc-certificate.vue')
    }]
  },
  {
    path: '/system/ram/detail/:userId',
    component: Layout,
    children: [
      {
        path: '',
        name: 'ram-detail',
        component: () => import('@/views/system/ram/detail.vue')
      }
    ]
  },
  {
    path: '/pass-design',
    component: Layout,    
    hidden: true,
    redirect: '/pass-template',
    meta: { noPadding: true },
    children: [
      {
        path: ':id',
        name: 'PassDesign',
        component: () => import('@/views/pass-center/design/index.vue')
      }
    ]
  },
  {
    path: '/topic',
    component: Layout,  
    hidden: true,
    children: [
      {
        path: '',
        name: 'Topic',
        component: () => import('@/views/pass-center/topic/index.vue')
      },
      {
        path: 'all',
        name: 'TopicAll',
        component: () => import('@/views/pass-center/topic/all.vue'),
        meta: {
          noPadding: true
        }
      },
      {
        path: ':id',
        name: 'TopicDetail',
        component: () => import('@/views/pass-center/topic/topic-detail.vue'),
        meta: {
          noPadding: true
        }
      }
    ]
  }
]

export const childrenRoutes = [

]

const router = createRouter({
  // 创建一个 hash 历史记录。
  routes: constantRoutes,
  history: createWebHashHistory('/operator/'),
  scrollBehavior: () => ({ left: 0, top: 0 })
})

// console.log(router)

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !WHITE_LIST_NAMES.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
  store.dispatch('permission/resetRoutes')
}

export function setupRouter(app) {
  app.use(router)
}

export default router
